import {T} from 'react-toast-mobile';
import {notify as reactNotify, clear as reactNotifyClear} from 'components/plugins/notify/react-notification';

window.T = T;

export function alert(msg, options) {
    return new Promise(resolve => {
        T.alert({
            title: options?.title || '提示',
            message: msg + '',
            text: options?.text || '确定',
            fn: () => resolve()
        });
    });
}

export function confirm(title, msg, options) {
    return new Promise(resolve => {
        T.confirm({
            title: title,
            message: msg,
            option: [{
                text: options?.cancel?.text || '取消',
                classNames: options?.className || 'gray',
                fn: () => resolve(false)
            }, {
                text: options?.ok?.text || '确定',
                classNames: options?.className,
                fn: () => resolve(true)
            }]
        });
    });
}

export function loading(msg) {
    T.clear();
    return T.loading(msg);
}

export function loaded() {
    return T.loaded();
}

export async function notify(msg, options = {}) {
    reactNotifyClear();

    if (!msg) {
        return null;
    }
    return reactNotify(msg, options);
}

export function clear() {
    T.clear();
    reactNotifyClear();
}